﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.ApproveFlow.Buz.Interfaces;
using Microhard.Core.DBUtility;
using Microhard.ApproveFlow.Buz.DataAccess;
using Microhard.ApproveFlow.Buz.Msg;
using Microhard.ApproveFlow.Buz.Models;
using Microhard.ApproveFlow.Buz.DTO;
using Microhard.Core;


namespace Microhard.ApproveFlow.Buz.Impls
{
    public class ApproveFlowTypeImpl : IApproveFlowType
    {
        public Core.DBUtility.ExecutedResult Add(Models.ApproveFlowTypeModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicroHardApproveFlowEntities db = new MicroHardApproveFlowEntities();

            var op = model.ToEntity();
            op.CreateDate = DateTime.Now;

            try
            {
                db.t_WorkFlowType.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, ApproveFlowTypeMsg.AddFailed);
                Log.Error("IApproveFlowType.Add", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Update(Models.ApproveFlowTypeModel model)
        {
            ExecutedResult result = new ExecutedResult();
            MicroHardApproveFlowEntities db = new MicroHardApproveFlowEntities();
            var entity = db.t_WorkFlowType.SingleOrDefault(l => l.TypeId == model.TypeId);
            if (entity != null)
            {
                entity.TypeName = model.TypeName;
                entity.TypeRemark = model.TypeRemark;
                entity.CreateDate = DateTime.Now;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, ApproveFlowTypeMsg.UpdateFailed);
                Log.Error("IApproveFlowType.Update", ex);
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            MicroHardApproveFlowEntities db = new MicroHardApproveFlowEntities();
            var entity = db.t_WorkFlowType.Where(l => ids.Contains(l.TypeId));
            foreach (var e in entity)
            {
                if (e.t_FlowChart.Count > 0)
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, string.Format(ApproveFlowTypeMsg.IsUsing, e.TypeName));
                }
                else
                {
                    db.t_WorkFlowType.DeleteObject(e);
                }
            }
            if (result.ErrorMsg.Count == 0)
            {
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, ApproveFlowTypeMsg.DeleteFailed);
                    Log.Error("IApproveFlowType.Delete", ex);
                }
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Delete(Models.ApproveFlowTypeModel model)
        {
            throw new NotImplementedException();
        }

        public Models.ApproveFlowTypeModel GetModelByID(Guid id)
        {
            throw new NotImplementedException();
        }

        public Models.ApproveFlowTypeModel GetModelByModel(Models.ApproveFlowTypeModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.ApproveFlowTypeModel> Query(Models.ApproveFlowTypeModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            throw new NotImplementedException();
        }

        public List<Models.ApproveFlowTypeModel> Query(Models.ApproveFlowTypeModel Model)
        {
            List<ApproveFlowTypeModel> result = new List<ApproveFlowTypeModel>();
            MicroHardApproveFlowEntities db = new MicroHardApproveFlowEntities();
            var entities = db.t_WorkFlowType.OrderByDescending(s => s.TypeName).ToList();
            if (entities.Count > 0)
            {
                result = entities.Select(p => p.ToModel()).ToList();
            }
            return result;
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }

        public bool IsTypeNameDuplicate(ApproveFlowTypeModel model)
        {
            bool result = false;
            MicroHardApproveFlowEntities db = new MicroHardApproveFlowEntities();
            var cnt = db.t_WorkFlowType.SingleOrDefault(s => s.TypeName == model.TypeName);
            if (cnt != null)
            {
                if (cnt.TypeId == model.TypeId)
                {
                    return result;
                }
                else
                {
                    result = true;
                }
            }
            return result;
        }
    }
}
